/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.atomojo.app;

import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;
import org.atomojo.app.db.Feed;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.ServerResource;

/**
 *
 * @author alex
 */
public class AdHocQueryResource extends ServerResource {

   public AdHocQueryResource()
   {
      setNegotiated(false);
   }

   public Representation get() {
      getResponse().setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
      return null;
   }

   public Representation post(Representation rep) {
      if (rep==null) {
         getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
         return new StringRepresentation("No entity body.");
      }
      if (!rep.getMediaType().equals(AtomResource.XQUERY_TYPE, true)) {
         getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
         return new StringRepresentation("Incorrect media type for query: "+rep.getMediaType());
      }

      Storage storage = (Storage)getContext().getAttributes().get(AtomApplication.STORAGE_ATTR);
      App app = (App)getContext().getAttributes().get(AtomApplication.APP_ATTR);

      String uriPath = getRequest().getResourceRef().getRemainingPart();
      try {
         String queryText = rep.getText();
         Storage.Query query = storage.compileQuery(queryText);
         if (uriPath==null) {
            uriPath = "";
         }
         if (uriPath.equals("/")) {
            uriPath = "";
         }
         int q = uriPath.indexOf('?');
         if (q>=0) {
            uriPath = uriPath.substring(0,q);
         }
         Feed f = app.getFeed(uriPath);
         if (f==null) {
            getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
            return null;
         }
         Form parameters = getRequest().getResourceRef().getQueryAsForm();
         Map<String,String> queryParameters = null;
         if (parameters!=null && parameters.size()>0) {
            queryParameters = new TreeMap<String,String>();
            for (String name : parameters.getNames()) {
               queryParameters.put(name,parameters.getValues(name));
            }
         }
         Representation result = storage.queryCollection(uriPath, f.getUUID(), query, queryParameters);
         result.setMediaType(MediaType.APPLICATION_XML);
         getResponse().setStatus(Status.SUCCESS_OK);
         return result;
      } catch (AppException ex) {
         if (ex.getStatus()!=null && ex.getStatus().getCode()==404) {
            getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
            return new StringRepresentation("No feed at path "+uriPath);
         } else {
            getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            return new StringRepresentation("App error: "+ex.getMessage());
         }
      } catch (IOException ex) {
         getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
         return new StringRepresentation("Query error: "+ex.getMessage());
      }
   }
}
